package suanfa.mathematics;

public class Main {
    public static void main(String[] args) {
        int n1=15;
        int n2=16;
//        boolean flag1=isPower(n1);
//        boolean flag2=isPower(n2);

//        boolean flag1=isPower2(n1);
//        boolean flag2=isPower2(n2);

        boolean flag1=isPower3(n1);
        boolean flag2=isPower3(n2);

        if(flag1){
            System.out.println(n1+"是否个自然数的二次方");
        }else{
            System.out.println(n1+"不是否个自然数的二次方");
        }

        if(flag2){
            System.out.println(n2+"是否个自然数的二次方");
        }else{
            System.out.println(n2+"不是否个自然数的二次方");
        }
    }

    public static boolean isPower(int n){
        int i;
        int m;

        if(n<0){
            System.out.println(n+"不是自然数");
        }

        for(i=1;i<n;i++){
            m=i*i;
            if(m==n){
                return true;
            }else if (m>n){
                return false;
            }

        }
        return false;
    }

    public static boolean isPower2(int n){
        int low=1;
        int high=n;
        int mid;

        int power;

        while(low <high){
            mid=(low+high)/2;
            power=mid*mid;


            if(power>n){
                //在low-（mid-1）继续查找
                high=mid-1;
            }else if(power<n){
                //在(mid+1)-high继续查找
                low=mid+1;
            }else{
                return true;
            }
        }
        return false;
    }

    public static boolean isPower3(int n){

        int minus=1;

        while(n>0){
            n=n-minus;

            //n是某个自然数的二次方
            if(n==0){
                return true;
            }
            else if(n<0){
                return false;
            }
            else{
                minus+=2;
            }
        }
        return false;
    }
}



